// 用户信息仓库
export default {
  state: {
    // 登录状态
    loginStatus: false,
    // token
    token: null,
    // 头像 昵称
    userInfo: {}
  },
  mutations: {
    // 一进去项目就读取该方法 检测用户是否登录
    initUser (state) {
      const userInfo = JSON.parse(localStorage.getItem('userInfo'))
      if (userInfo) {
        state.loginStatus = true
        state.token = userInfo.token
        state.userInfo = userInfo
      }
    },
    login (state, user) {
      state.loginStatus = true
      state.token = user.token
      state.userInfo = user

      // 持久化存储
      localStorage.setItem('userInfo', JSON.stringify(user))
    },
    loginOut (state) {
      state.loginStatus = false
      state.token = null
      state.userInfo = {}
      localStorage.removeItem('userInfo')
    }
  }
}
